package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.system.ApplicationDomain;
	import flash.utils.getDefinitionByName;
	
	import utils.Button;
	import utils.Debuger;
	import utils.UIHelper;
	import utils.events.MenuEvent;

	[SWF(backgroundColor="0x555555", width="509", height="323")]
	public class DeskTable extends Sprite
	{
		public var menuArrays:Array=[];
		private var menuSartX:Number=0;
		private var menuSartY:Number=21;
		private var menuContentX:Number=400;
		private var menuContentY:Number=0;
		public var curentContent:DeskMenuItem;
		private var lastMenuX:int;
		private var lastMenuY:Number=0;
		public var menuContainer:Sprite=new Sprite();
		private var scroll:ScrollBarSprite;

		public function DeskTable()
		{
			super();
			this.x=146;
			this.y=67;
			initMenu();
			initScroll();
			//testScroll();
			UIHelper.addBitmapDataButtonChild(this, btn_closedesk_up, btn_closedesk_over, onDeskMunuCloseClickHandler, 479,3,true);
		}
		
		private function onDeskMunuCloseClickHandler(event:MouseEvent):void
		{
			// TODO Auto Generated method stub
			trace("desk table btn close");
			var curMainmenu:MainMenuItem=this.parent["curentContent"] as MainMenuItem;
			if(curMainmenu)
			curMainmenu.dispatchEvent(new MenuEvent(MenuEvent.onDeskTableCloseClick, "")); 
		}
		
		private function initScroll():void
		{
			var line:Sprite=new Sprite();
			addBitmapChild(line, Desk_Scroll);
			var btn:Sprite=new Sprite();
			addBitmapChild(btn, Btn_Desk_Scroll);
			scroll=new ScrollBarSprite(line, btn, menuContainer, 493, 296, 4, 4);
			//添加到滚动条容器，初始化地方变了
			scroll.x=7;
			scroll.y=24;
			addChild(scroll);
		}

		private function testScroll():void
		{
			var bgBitMapData:BitmapData=new background as BitmapData;
			addChild(new Bitmap(bgBitMapData));
			var line:Sprite=new Sprite();
			addBitmapChild(line, Desk_Scroll);
			var btn:Sprite=new Sprite();
			addBitmapChild(btn, Btn_Desk_Scroll);
			var content:Sprite=new Sprite();
//			content.graphics.beginFill(0x344523);
//			content.graphics.drawRect(0,0,480,600);
//			content.graphics.endFill();
			addBitmapChild(content, pear);
			trace("content height:" + content.height);
			addBitmapChild(content, apple, 0, content.getChildAt(0).height);
			trace("content height:" + content.height);
			var scroll:ScrollBarSprite=new ScrollBarSprite(line, btn, content, 493, 296, 4, 4);
			//添加到滚动条容器，初始化地方变了
			scroll.x=7;
			scroll.y=24;
			addChild(scroll);
			content.removeChildAt(0);
			content.getChildAt(0).y=0;
			trace("content height:" + content.height);
		}

		private function addBitmapChild(parentSprite:Sprite, bitmapdataclz:Class, sx:int=0, sy:int=0, boolVisible:Boolean=true):void
		{
			if (!parentSprite || !bitmapdataclz)
			{
				return;
			}
			var bitmap:Bitmap=new Bitmap(new bitmapdataclz as BitmapData);
			bitmap.x=sx;
			bitmap.y=sy;
			bitmap.visible=boolVisible;
			parentSprite.addChild(bitmap);
		}

		private function testMenuContent():void
		{
			var content:MenuContent=new MenuContent;
			addChild(content);
		}

		protected function onMenuItemClickHandler(event:MenuEvent):void
		{
			// TODO Auto-generated method stub
			trace("item click");
			scroll.dispatchEvent(event);
		}

		private function testButton():void
		{
			try
			{
				var btn:Button=new Button(Vedio_btn, Vedio_btn_over, null);
				addChild(btn);
			}
			catch (error:Error)
			{
				trace(error);
			}

//			var btn:Button=new Button("Menu");
//			addChild(btn);
		}

		private function initMenu():void
		{
			// 初始化背景素材
			var bgBitMapData:BitmapData=new background as BitmapData;
			addChild(new Bitmap(bgBitMapData));
			//添加到滚动条容器，初始化地方变了
//			menuContainer.x=5;
//			menuContainer.y=0;
//			addChild(menuContainer);

			//初始化菜单		
//			menuArrays.push(new DeskMenuItem("menu1", Menubtn, Menubtnover, new MenuContent(["1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2"],["0.1", "0.2", "0.1", "0.2", "0.1", "0.2", "0.1", "0.2", "0.1", "0.2", "0.1", "0.2"],["4.5", "2.8", "50.6", "5.6", "2.8", "50.6", "9.6", "2.8", "50.6", "4.6", "2.8", "50.6"])));
//			menuArrays.push(new DeskMenuItem("menu1", Menubtn, Menubtnover, new MenuContent(["3", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2"],["0.2", "0.2", "0.1", "0.2", "0.1", "0.2", "0.1", "0.2", "0.1", "0.2", "0.1", "0.2"],["4.6", "2.8", "50.6", "6.6", "2.8", "50.6", "14.6", "2.8", "50.6", "4.6", "2.8", "50.6"])));
//			menuArrays.push(new DeskMenuItem("menu1", Menubtn, Menubtnover, new MenuContent(["4", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2"],["0.3", "0.2", "0.1", "0.2", "0.1", "0.2", "0.1", "0.2", "0.1", "0.2", "0.1", "0.2"],["4.7", "2.8", "50.6", "7.6", "2.8", "50.6", "12.6", "2.8", "50.6", "4.6", "2.8", "50.6"])));
//			menuArrays.push(new DeskMenuItem("menu1", Menubtn, Menubtnover, new MenuContent(["5", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2"],["0.4", "0.2", "0.1", "0.2", "0.1", "0.2", "0.1", "0.2", "0.1", "0.2", "0.1", "0.2"],["4.8", "2.8", "50.6", "8.6", "2.8", "50.6", "13.6", "2.8", "50.6", "4.6", "2.8", "50.6"])));
			for (var j:int=0; j < 12; j++)
			{
				initMenuArray("menu" + j, ["1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2"], ["0.1", "0.2", "0.1", "0.2", "0.1", "0.2", "0.1", "0.2", "0.1", "0.2", "0.1", "0.2"], ["4.5", "2.8", "50.6", "5.6", "2.8", "50.6", "9.6", "2.8", "50.6", "4.6", "2.8", "50.6"]);
			}

			//501*200掩码
			//lastMenuX=0;
			for (var i:int=0; i < menuArrays.length; i++)
			{
				var btn:Sprite=DeskMenuItem(menuArrays[i]).menuContainer;
				btn.y=lastMenuY;
				//btn.x=this.menuSartX;
				this.menuContainer.addChild(btn);
				lastMenuY+=btn.height;

			}
		}

		private function initMenuArray(menuName:String, resultBall:Array=null, rateBall:Array=null, scoreResult:Array=null):void
		{
			// TODO Auto Generated method stub
			this.menuArrays.push(new DeskMenuItem(menuName, Menubtn, Menubtnover, new MenuContent(resultBall, rateBall, scoreResult)));
		}

	}
}
